package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class FileNioReader {
	 private static String getCellValue(Cell cell) {
	        Object result = "";
	        if (cell != null) {
	            switch (cell.getCellType()) {
	                case STRING:
	                    result = cell.getStringCellValue();
	                    break;
	                case NUMERIC:
	                    result = cell.getNumericCellValue();
	                    break;
	                case BOOLEAN:
	                    result = cell.getBooleanCellValue();
	                    break;
	                case FORMULA:
	                    result = cell.getCellFormula();
	                    break;
	                case ERROR:
	                    result = cell.getErrorCellValue();
	                    break;
	                case BLANK:
	                    break;
	                default:
	                    break;
	            }
	        }
	        return result.toString();
	    }

	    public static String read(String filepath) throws IOException {
	        if (filepath.endsWith(".txt")) {
	            File file = new File(filepath);
	            FileInputStream fileInputStream = new FileInputStream(file);
	            FileChannel fileChannel = fileInputStream.getChannel();
	            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
	            fileChannel.read(byteBuffer);
	            return new String(byteBuffer.array()) + "\n.txt";
	        } else if (filepath.endsWith(".json")) {
	            File file = new File(filepath);
	            FileInputStream fileInputStream = new FileInputStream(file);
	            FileChannel fileChannel = fileInputStream.getChannel();
	            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
	            fileChannel.read(byteBuffer);
	            return new String(byteBuffer.array());
	        } else {
	            StringBuilder stringBuilder = new StringBuilder();
	            File file = new File(filepath);
	            Workbook workbook = new XSSFWorkbook(new FileInputStream(file));
	            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
	                Sheet sheet = workbook.getSheetAt(i);

	                for (int j = 0; j <= sheet.getLastRowNum(); j++) {
	                    Row row = sheet.getRow(j);
	                    if (row != null) {
	                        for (int k = 0; k < row.getLastCellNum(); k++) {
	                            Cell cell = row.getCell(k);
	                            String cellValue = getCellValue(cell);
	                            if (!cellValue.equals("")) {
	                                if (k != row.getLastCellNum() - 1)
	                                    stringBuilder.append(cellValue + " ");
	                                else {
	                                    stringBuilder.append(cellValue);
	                                }
	                            }

	                        }
	                        stringBuilder.append("\n");
	                    }
	                }
	            }
	            return new String(stringBuilder) + ".xlsx";

	        }
	    }

	}
